package com.missmess.opengldemo.ui;

import android.os.Bundle;

import com.missmess.opengldemo.GlBaseActivity;
import com.missmess.opengldemo.util.BufferUtils;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class DrawTriangle extends GlBaseActivity {
    float vertexArray[] = {
            -0.8f, -0.4f, 0.0f,
            0.0f, -0.4f, 0.0f,
            -0.4f, 0.4f, 0.0f,

            0.0f, -0.0f, 0.0f,
            0.8f, -0.0f, 0.0f,
            0.4f, 0.4f, 0.0f,
    };
    private FloatBuffer buffer;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        buffer = BufferUtils.float2Buffer(vertexArray);
    }

    @Override
    public void drawScene(GL10 gl) {
        super.drawScene(gl);
        gl.glTranslatef(0, 0, -4f);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glPointSize(12);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, buffer);
        index++;
        index %= 300;
        if(index < 100) {
            gl.glColor4f(1, 0, 0, 1);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
        } else if(index < 200) {
            gl.glColor4f(0, 1, 0, 1);
            gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
        } else {
            gl.glColor4f(0, 0, 1, 1);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
        }

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}
